/**
 * Copyright (c) 2009 Jim Hurne
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package org.tzconverter.tzgroup;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import org.tzconverter.AbstractManager;
import org.tzconverter.AppPrefs;
import org.tzconverter.menu.MenuView;
import org.tzconverter.tzdisplay.TimeZoneDisplayView;

/**
 * Manager (presenter) for the AddTimeZone component.
 * 
 * 
 * @since May 12, 2009
 */

public class TimeZoneGroupManager extends AbstractManager
{
  private final TimeZoneGroupView view;
  private final TimeZoneGroupModel model;
  
  public TimeZoneGroupManager(TimeZoneGroupModel model, TimeZoneGroupView view, MenuView menuView)
  {
    super(menuView);
    this.view = view;
    this.model = model;
    
    registerListeners();
  }
  
  public void afterConstructed()
  {
    whenLoadClicked();
    
    view.setAddButtonEnabled(false);    
  }
  
  private void whenAddButtonClicked()
  {
    //Get the timeZoneId from the view, and pass it to the model
    String timeZoneId = view.getTimeZoneId();
    TimeZoneDisplayView tzDisplayView = model.createTimeZoneDisplay(timeZoneId);
    view.addTimeZoneDisplay(tzDisplayView);
    
    //Clear the timeZoneId text field
    //Note that this will generate another text changed event.
    view.setTimeZoneId("");
  }
  
  private void whenTimeZoneIdChanged(String timeZoneId)
  {
    //An empty time zone id is invalid,
    //but an error should not be displayed.
    if("".equals(timeZoneId))
    {
      view.clearError();
      view.setAddButtonEnabled(false);
    }
    else if(model.isValidTimeZoneId(timeZoneId))
    {
      view.clearError();
      view.setAddButtonEnabled(true);
    }
    else 
    {
      view.showError("Invalid TimeZone ID");
      view.setAddButtonEnabled(false);
    }
  }
  
  private void whenTimeZoneDisplayRemoved(TimeZoneDisplayView tzDisplayView)
  {
    model.removeTimeZoneDisplay(tzDisplayView);
  }
  
  @Override
  protected void whenLoadClicked()
  {
    // Each TimeZoneDisplayView will remove itself, so we don't
    // need to do it here.
    List<String> ids = model.getSavedTimeZoneIds();
    
    for(String id: ids)
    {
      TimeZoneDisplayView tzDisplayView = model.createTimeZoneDisplay(id);
      view.addTimeZoneDisplay(tzDisplayView);
    }    
  }
  
  @Override
  protected void whenLoadDefaultsClicked()
  {
    // Each TimeZoneDisplayView will remove itself, so we don't
    // need to do it here.
    for(String id: AppPrefs.DEFAULT_IDs)
    {
      TimeZoneDisplayView tzDisplayView = model.createTimeZoneDisplay(id);
      view.addTimeZoneDisplay(tzDisplayView);
    }    
  }

  @Override
  protected void whenSaveClicked()
  {
    model.saveCurrentTimeZoneIds();
  }

  private void registerListeners() {
    view.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent event)
      {
        whenAddButtonClicked();
      }
      
    });
    
    view.addPropertyChangeListener(new PropertyChangeListener() {
      @Override
      public void propertyChange(PropertyChangeEvent event)
      {
        if(TimeZoneGroupView.PROP_TIME_ZONE_ID.equals(event.getPropertyName()))
        {
          whenTimeZoneIdChanged((String) event.getNewValue());
        }
      }
    });
    
    view.addTimeZoneRemoveListener(new TimeZoneRemoveListener() {
      @Override
      public void timeZoneRemoved(TimeZoneDisplayView removed)
      {
        whenTimeZoneDisplayRemoved((TimeZoneDisplayView) removed);
      }      
    });
  }
}
